#!/bin/bash
function judge_stop(){
   XX=$(grep  'Stopped JobGenerator'  $1)
   NN=$(date +%m%d%H%M%S) 
if [ "$XX" != "" ]
   then
   str=$2
   LENGTH=${#str}
   topic=${str:0:$LENGTH-4}
   process=`ps -ef|grep  topic=$topic  |grep -v 'grep'| awk '{print $2}'`
   `kill -9 $process`
   `mv $1 $1.$NN`
  CWD=`pwd`
  SP=""
  CP=.
  for f in $CWD/lib/*.jar
  do
        CP="$CP":$f
        if [ -z $SP ]; then
          SP=file:$f
        else
          SP="$SP",file:$f
        fi
  done
  nohup java -Dfile.encoding=utf-8 -Xmx8g -cp $CP com.wenge.datagroup.storage.main.SparkStreamConsumer $topic  --config=config/config.properties --topic=$topic  --sparkConf=config/spark-default.properties  >> $1 & 
fi
}
ABC=logs
A=''
B=''
for i in `ls $ABC`
do
 if [ -d $ABC"/"$i ];
 then
  A=$i
    for j in `cd $ABC;ls $A`
    do
    B=$j
    qian=${B:0:5}
    hou=${B:0-3:3}
    if [ "$hou" = "log" ] && [ "$qian" = "topic" ]
    then
    judge_stop $ABC"/"$A"/"$B $B
    fi
    done
  else
    B=$i
    qian=${B:0:5}
    hou=${B:0-3:3}
    if [ "$hou" = "log" ] && [ "$qian" = "topic" ]
    then
    judge_stop $ABC"/"$B $B
    fi
  fi
done
